//
//  NickVC.m
//  BrushFace
//
//  Created by Harry on 16/1/24.
//  Copyright © 2016年 Zzz. All rights reserved.
//
#import "MeRequest.h"
#import "NickVC.h"
#import "nickView.h"
#import "HangYeView.h"
@interface NickVC ()
@property(nonatomic,strong)HangYeView *nickView;
@property(nonatomic,strong)nickView   *hangye;
@end

@implementation NickVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
   
    rightBtn.frame = CGRectMake(SCREEN_WIDTH-50, 30, 40, 30);
    [self.navBar addSubview:rightBtn];
   
    switch (_VCType)
    {
            
        case nick:
        {
             [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
            [self.navBar setTitle:@"昵称"];
            
            //把昵称和公司行业的名字写反了
            HangYeView *hangye = [[[NSBundle mainBundle]loadNibNamed:@"HangYeView" owner:self options:nil]lastObject];
            hangye.frame = CGRectMake(0, 70, SCREEN_WIDTH, 40);
            [self.view addSubview:hangye];
            self.nickView = hangye;
           
        }
            break;
        case hangYe:
        {
            [self.navBar setTitle:@"公司-职业"];
           [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
            nickView *nickName = [nickView loadBundleNib];
            nickName.frame = CGRectMake(0, 70, SCREEN_WIDTH, 80);
            [self.view addSubview:nickName];
            self.hangye = nickName;
        }
            break;
        default:
            break;
    }
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];

    
   }

-(void)rightBtnClick
{
    switch (_VCType)
    {
        case nick:
        {
            if (_nickView.textfield.text.length>0)
            {
                if (_nickView.textfield.text.length>10) {
                    [[DialogUtil sharedInstance] showDlg:self.view textOnly:@"最多10个字"];
                    return;
                }
                [self sendNickName];
            }
            else
            {

             [[DialogUtil sharedInstance] showDlg:self.view textOnly:@"昵称不能为空"];
            }
            
        }
            break;
        case hangYe:
        {
            if (_hangye.textCompany.text.length>0 && _hangye.textCalling.text.length>0)
            {
                [self sendCompanyInfo];
            }
            else
            {
                [[DialogUtil sharedInstance] showDlg:self.view textOnly:@"数据不能为空"];

  
            }
        }
            break;
  
        default:
            break;
    }
    
    
}

-(void)sendCompanyInfo
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:self.hangye.textCalling.text forKey:@"profession"];
    [params setObject:self.hangye.textCompany.text forKey:@"companyname"];
    [MeRequest sendUserDataWithType:company AndUserData:params success:^(NSDictionary *dic)
     {
         dispatch_async(dispatch_get_main_queue(), ^{
             NSString *string = [NSString stringWithFormat:@"%@",dic[@"result"]];
             if ([string intValue]==1)
             {
                 _result(_hangye.textCalling.text);
                [self.navigationController popViewControllerAnimated:YES];
             }});
         
     } error:^(NSError *error) {
         
     }];

    
   
}


-(void)sendNickName
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:self.nickView.textfield.text forKey:@"nickname"];
    [MeRequest sendUserDataWithType:nickName AndUserData:params success:^(NSDictionary *dic)
     {
         dispatch_async(dispatch_get_main_queue(), ^{
             NSString *string = [NSString stringWithFormat:@"%@",dic[@"result"]];
             if ([string intValue]==1)
             {
                 _result(_nickView.textfield.text);
                 [self.navigationController popViewControllerAnimated:YES];
             }});
     } error:^(NSError *error) {
         
     }];


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
